package com.miyu.system.service.book.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.pojo.book.BookPage;
import com.miyu.common.service.book.BookService;
import com.miyu.common.utils.magic.GetBeanUtil;
import com.miyu.system.dao.book.BookMainDao;
import com.miyu.common.pojo.system.SysRule;
import com.miyu.system.service.rule.SysRuleService;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * 缓存书任务
 *
 * @author MIYU
 */
public class CacheBookTask implements Callable<Boolean> {
    BookMainDao dao = (BookMainDao) GetBeanUtil.getBeanByClass(BookMainDao.class);
    SysRuleService sysRuleService = (SysRuleService) GetBeanUtil.getBeanByClass(SysRuleService.class);
    BookService bookService = (BookService) GetBeanUtil.getBeanByClass(BookService.class);
      ScheduledExecutorService jXPage = new ScheduledThreadPoolExecutor(2, new BasicThreadFactory.Builder()
            .namingPattern("JXReadThead-%d").daemon(true).build());
    private JSONObject bookInfo;
    private int type;
    @Override
    public Boolean call() throws Exception {
        switch (type) {
            //接章节解析
            case 1:
                 saveReader();
                break;
                //多线程解析
            case 2:
               JXReader();
                break;
            default:
                break;
        }

        return false;
    }

    private void JXReader() {
        String ruleId = bookInfo.getString("ruleId");
        SysRule sysRule = sysRuleService.getSysRuleById(ruleId);
        BookPage bookPage = new BookPage();
        bookPage.setRuleId(ruleId);
        bookPage.setUrl(bookInfo.getString("chapterSource"));
        bookPage.setRemove(true);
        String tableId=bookInfo.getString("tableId");
        bookPage.setRule(JSONObject.parseObject(sysRule.getRuleSet()));
        //解析内容
        JSONObject jsonObject = bookService.queryBookReader(bookPage);
        // 进行保存数据
        JSONObject cpt = new JSONObject(jsonObject);
        cpt.put("chapterTable",tableId);
        cpt.put("chapterContext",jsonObject.getString("chapterContext"));
        cpt.put("fpk",bookInfo.getString("fpk"));
        executorService.submit(new BookSaveTaskJob(cpt,"1"));
    }


    /**
     * 读取章节信息 缓存阅读表数据
     * @return {@link Boolean}
     */    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(15, new BasicThreadFactory.Builder()
            .namingPattern("saveReadThead-%d").daemon(true).build());
    private Boolean saveReader() {
        StringBuffer sql = new StringBuffer();
        String fpk = bookInfo.getString("fpk");
        String tableId = bookInfo.getString("bookChapterSource");
        String ruleId = bookInfo.getString("ruleId");
        sql.append("select * from book_chapter_"+tableId+" a ");
        sql.append(" where  ");
        sql.append(" not exists (select 1 from book_reader_"+tableId+" b where  a.fpk=b.fpk  )");
        sql.append(" and a.book_id ='").append(fpk).append("'");
        sql.append(" order by chapter_num  ");

        JSONArray chapterList = dao.queryJSONArrayBySql(sql.toString());
        if (chapterList.size()> 0) {
            for (int i = 0; i < chapterList.size(); i++) {
                JSONObject chapter = (JSONObject) chapterList.get(i);
                chapter.put("ruleId",ruleId);
                chapter.put("tableId",tableId);
                jXPage.submit(new CacheBookTask(chapter,2));
            }
        }
        return true;
    }

    public CacheBookTask(JSONObject bookInfo, int type) {
        this.bookInfo = bookInfo;
        this.type = type;

    }
}
